package com.flong.modules.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.flong.modules.AppException;
import com.flong.modules.BaseService;
import com.flong.modules.model.oracle.Sequences;
import com.flong.modules.utils.DBHelper;
import com.flong.modules.utils.ObjectUtil;

/**
 * 
 * @Description	序列
 * @ClassName	SequencesHelper
 * @Date		2017年2月23日 下午3:49:39
 * @Author		liangjilong
 * @Copyright (c) All Right Reserved , 2017.
 */
@SuppressWarnings("all")
public class SequencesService extends BaseService {
 
	private static String SELECT_SEQUENCE_ORACLE_SQL = "select sequence_name,min_value,max_value,increment_by,cycle_flag,order_flag,cache_size,last_number from user_sequences ";
	/**
	 * @Description 获取所有数据库序列
	 * @Author		liangjilong
	 * @Date		2017年2月23日 下午3:57:42
	 * @return
	 * @throws Exception 		参数
	 * @return 		List<Sequences> 返回类型
	 */
	public static List<Sequences> getSequences() throws Exception {
		conn = DBHelper.getInstance().getConnection(); //链接数据库
		if(ObjectUtil.isNotEmpty(dialect) && "ORACLE".equalsIgnoreCase(dialect)){
			sql =SELECT_SEQUENCE_ORACLE_SQL;//
		}else{
			throw new AppException(1,"方言配置有误，目前只支持Oracle");
		}
		List<Sequences> sequences = retSequenceList();
		return sequences;
	}

	private static List<Sequences> retSequenceList()  {
		List<Sequences> sequences = null;
		try {
			psmt = conn.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			sequences = DBHelper.convertToEntitys(rs, Sequences.class);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.release(conn, psmt, rs);
		}
		return sequences;
	}
	
	/**
	 * @Description 根据序列名称获取序列信息
	 * @Author		liangjilong
	 * @Date		2017年2月23日 下午4:25:08
	 * @param seqName
	 * @throws Exception 		参数
	 * @return 		List<Sequences> 返回类型
	 */
	public static List<Sequences> getSequencesByName(String seqName) throws Exception {
		conn = DBHelper.getInstance().getConnection(); //链接数据库
		if(ObjectUtil.isNotEmpty(dialect) && "ORACLE".equalsIgnoreCase(dialect)){
			sql = SELECT_SEQUENCE_ORACLE_SQL+" where upper(sequence_name)=upper('"+seqName+"')";//
		}else{
			throw new AppException(1,"方言配置有误，目前只支持Oracle");
		}
		psmt = conn.prepareStatement(sql);
		ResultSet rs = psmt.executeQuery();
		List<Sequences> sequences =  DBHelper.convertToEntitys(rs, Sequences.class);
		DBHelper.release(conn, psmt, rs);
		return sequences;
	}
	/**
	 * @Description 获取条数
	 * @Author		liangjilong
	 * @Date		2017年3月1日 下午1:31:29
	 * @param seqName
	 * @return
	 * @throws Exception 		参数
	 * @return 		int 返回类型
	 */
	public static int  getCountSequencesByName(String seqName) throws Exception {
		int result = -1;
		conn = DBHelper.getInstance().getConnection(); //链接数据库
		if(ObjectUtil.isNotEmpty(dialect) && "ORACLE".equalsIgnoreCase(dialect)){
			sql = "select count(*) as num  from user_sequences where upper(sequence_name)=upper('"+seqName+"')";//
		}else{
			throw new AppException(1,"方言配置有误，目前只支持Oracle");
		}
		result = getNumFieldValue(result, sql);
		return result;
	}

	
	public static void main(String[] args)throws Exception {
		
		List<Sequences> sequences = getSequences();
		for(Sequences seq:sequences){
			System.out.println(seq);
			
		}
		
	}
	
	 
}
